package cn.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.common.ExceResult;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.entity.attendance.AttDetailEntity;
import cn.kpi.entity.attendance.AttSummaryEntity;
import cn.kpi.entity.attendance.AttTbBatchEntity;
import cn.kpi.entity.sys.OrgEntity;
import cn.kpi.entity.sys.UserEntity;
import cn.kpi.entity.usualassessment.UsAssTempCreateIndexEntity;
import cn.kpi.entity.usualassessment.UsAssTemplateEntity;
import cn.kpi.entity.vtiondpline.VtDpItemEntity;
import cn.kpi.entity.vtiondpline.VtDpUserListEntity;
import cn.kpi.service.attendance.IAttTbService;
import cn.kpi.service.common.IOrgSummaryService;
import cn.kpi.service.sys.IUserService;

@Controller
@RequestMapping("atttb")
public class AttTbController extends BaseController {
	
	@Autowired
	private IUserService iUserService;
	
	@Autowired
	private IAttTbService iAttTbService;
	
	@Autowired
	private IOrgSummaryService iOrgSummaryService;

	@RequestMapping(value = "/att_tb_list")
	public String AttTbList(Model model) {
		Calendar cale = Calendar.getInstance();
		int year = cale.get(Calendar.YEAR);
		model.addAttribute("year", year);
		return "attendance/att_tb_list";
	}
	
	@RequestMapping(value = "/att_tb_my")
	public String AttTbMy(Model model) {
		try{
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH )+1;
			
			model.addAttribute("year", year);
			model.addAttribute("month", month);
		}
		catch(Exception ex){
			
		}
		return "attendance/att_tb_my";
	}
	
	@RequestMapping(value = "/att_tb_edit")
	public String AttTbEdit(Model model) {
		try{
			Calendar cale = Calendar.getInstance();
			int year = cale.get(Calendar.YEAR);
			model.addAttribute("year", year);
			String where=this.GetLoginUserForUserListWhere();
			List<UserEntity> userlst = iUserService.GetUserListByWhere(where);
			model.addAttribute("userlst", userlst);
		}
		catch(Exception ex){
			
		}
		return "attendance/att_tb_edit";
	}
	
	@RequestMapping(value = "/att_tb_edit_show")
	public String AttTbEditShow(Model model,Long userid,int year,int mouth) {
		try{
			UserEntity userentity = iUserService.GetUserById(userid);
			model.addAttribute("userentity", userentity);
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
			Calendar calendar = Calendar.getInstance();  
	        calendar.setTime(sdf.parse(year+"-"+mouth+"-1"));  
	        int countday=calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 
	        List<Integer> dayslist=new ArrayList<Integer>();
	        for(int i=0;i<countday;i++){
	        	dayslist.add((i+1));
	        }
	        model.addAttribute("dayslist", dayslist);
	        
	        model.addAttribute("userid", userid);
	        model.addAttribute("year", year);
	        model.addAttribute("mouth", mouth);
		}
		catch(Exception ex){
			
		}
		return "attendance/att_tb_edit_show";
	}
	
	
	@RequestMapping(value = "/add_atttb")
	public String AddAttTb(HttpServletRequest request, HttpServletResponse response) {
		try {
			String struserid = request.getParameter("userid");
			String stryear = request.getParameter("year");
			String strmouth = request.getParameter("mouth");
			if(struserid==null||struserid.length()<=0){
				return renderStringScript(response, "<script>window.parent.CloseDialog(0,'参数有误')</script>");
			}
			
			Long userid=Long.parseLong(struserid);
			Long byuserid=this.GetLoginUserId();
			UserEntity byuser = this.GetUserById(byuserid);
			int year = Integer.parseInt(stryear);
			int mouth = Integer.parseInt(strmouth);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
			Calendar calendar = Calendar.getInstance();  
	        calendar.setTime(sdf.parse(year+"-"+mouth+"-1"));  
	        int countday=calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 
	        List<AttDetailEntity> lst=new ArrayList<AttDetailEntity>();
	        int sfqq=0;
	        for(int i=0;i<countday;i++){
	        	int day=i+1;
	        	AttDetailEntity item=new AttDetailEntity();
	        	item.setBj(Integer.parseInt(request.getParameter("bj_"+day)));//病假
	        	item.setCd(Integer.parseInt(request.getParameter("cd_"+day)));
	        	item.setDay(day);
	        	item.setHj(Integer.parseInt(request.getParameter("hj_"+day)));//婚假
	        	item.setKg(Integer.parseInt(request.getParameter("kg_"+day)));
	        	item.setMouth(mouth);
	        	item.setNxj(Integer.parseInt(request.getParameter("nxj_"+day)));//年休假
	        	item.setQt(Integer.parseInt(request.getParameter("qt_"+day)));//其他
	        	item.setSfcq(Integer.parseInt(request.getParameter("sfcq_"+day)));
	        	item.setSfjb(Integer.parseInt(request.getParameter("sfjb_"+day)));
	        	item.setShij(Integer.parseInt(request.getParameter("shij_"+day)));//事假
	        	item.setSj(Integer.parseInt(request.getParameter("sj_"+day)));//丧假
	        	item.setStatus(0);
	        	item.setSyj(Integer.parseInt(request.getParameter("syj_"+day)));//生育假
	        	item.setTqj(Integer.parseInt(request.getParameter("tqj_"+day)));//探亲假
	        	item.setUserid(userid);
	        	item.setYear(year);
	        	item.setYgzs(Integer.parseInt(request.getParameter("ygzs_"+day)));//因公致伤
	        	item.setZt(Integer.parseInt(request.getParameter("zt_"+day)));
	        	item.setRemark(request.getParameter("remark_"+day));
	        	item.setByuserid(byuserid);
	        	lst.add(item);
	        }

			int iResult=0;
			iResult=iAttTbService.InsertAttTb(lst);
			
			return renderStringScript(response, "<script>window.parent.CloseDialog(" + iResult + ")</script>");
		} catch (Exception ex) {
			return renderStringScript(response, "<script>window.parent.CloseDialog(0)</script>");
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/create_atttb")
	public ExceResult CreateAttTb(HttpServletRequest request, HttpServletResponse response) {
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try {
			String strorgid = request.getParameter("orgid");
			String stryear = request.getParameter("year");
			String strmonth = request.getParameter("month");
			
			Long orgid=Long.parseLong(strorgid);
			Long byuserid=this.GetLoginUserId();
			int year = Integer.parseInt(stryear);
			int month = Integer.parseInt(strmonth);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
			Calendar calendar = Calendar.getInstance();  
	        calendar.setTime(sdf.parse(year+"-"+month+"-1"));  
	        int countday=calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 
	        
	        
	        
	        AttTbBatchEntity entity=new AttTbBatchEntity();
	        entity.setByuserid(byuserid);
	        entity.setDay(0);
	        entity.setDays(countday);
	        entity.setMonth(month);
	        entity.setOrgid(orgid);
	        entity.setYear(year);
			switch (month) {
			case 1:
			case 2:
			case 3:
				entity.setQuarter(1);
				break;
			case 4:
			case 5:
			case 6:
				entity.setQuarter(2);
				break;
			case 7:
			case 8:
			case 9:
				entity.setQuarter(3);
				break;
			case 10:
			case 11:
			case 12:
				entity.setQuarter(4);
				break;
			}

			int iResult=0;
			iResult=iAttTbService.CreateInsertAttTb(entity);
			if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		} catch (Exception ex) {
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
			ex.printStackTrace();
		}
		return result;
	}
	
	
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list")
	public LayTable<AttSummaryEntity> GetList(HttpServletRequest request) {

		List<AttSummaryEntity> lst=null;
		LayTable<AttSummaryEntity> layTable=new LayTable<AttSummaryEntity>();
		try {
			String year = request.getParameter("year");
			String mouth = request.getParameter("mouth");
			String orgid = request.getParameter("orgid");
			String where="";
			if(year!=null&&year.length()>0){
				where+=" AND year="+year;
			}
            if(mouth!=null&&mouth.length()>0){
            	where+=" AND mouth="+mouth;
			}

            if(orgid!=null&&orgid.length()>0){
            	where+=" AND userid IN (SELECT userid FROM t_sys_user WHERE szdw="+orgid+")";
            }
            
            
			lst = iAttTbService.GetAttTbListByWhere(where);
			for (int i = 0; i < lst.size(); i++) {
				lst.get(i).setRowNum(i + 1);
			}
 
			int iCount=iAttTbService.GetAttTbListCountByWhere(where);
			layTable.setCode(0);
			layTable.setCount(iCount);
			layTable.setData(lst);
			
		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
	
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_org_summary_list")
	public LayTable<Map> GetOrgSummaryList(HttpServletRequest request) {

		List<Map> lst=null;
		LayTable<Map> layTable=new LayTable<Map>();
		try {
			int year = Integer.parseInt(request.getParameter("year"));
			int month = Integer.parseInt(request.getParameter("month"));
			String where="";
			
            //where+=this.GetLoginUserForOtherListWhereByUserId();
			where+=" AND orgid IN ("+this.GetLoginUserOrgidsForOrg()+")";
			lst = iOrgSummaryService.GetOrgSummaryList(where,year, month,"",0);
//			for (int i = 0; i < lst.size(); i++) {
//				lst.get(i).setRowNum(i + 1);
//			}
 
			//int iCount=iAttTbService.GetAttTbListCountByWhere(where);
			//System.out.println(JSONObject.toJSONString(lst));
			layTable.setCode(0);
			layTable.setCount(lst.size());
			layTable.setData(lst);
			
		} catch (Exception e) {
			e.printStackTrace();
			layTable.setCode(500);
		}
		return layTable;
	}
	
	
	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_my_list")
	public LayTable<AttSummaryEntity> GetMyList(HttpServletRequest request) {

		List<AttSummaryEntity> lst=null;
		LayTable<AttSummaryEntity> layTable=new LayTable<AttSummaryEntity>();
		try {
			int page=Integer.parseInt(request.getParameter("page"));
			int limit=Integer.parseInt(request.getParameter("limit"));
			
			String year = request.getParameter("year");
			String mouth = request.getParameter("mouth");
			String where="";
			if(year!=null&&year.length()>0){
				where+=" AND year="+year;
			}
            if(mouth!=null&&mouth.length()>0){
            	where+=" AND mouth="+mouth;
			}
            
            where+=" AND userid="+this.GetLoginUserId();
            
            int beginrow=(page-1)*limit;
			int endrow=limit;
            
            where+=" LIMIT "+beginrow+","+endrow;
 
			lst = iAttTbService.GetAttTbListByWhere(where);
			for (int i = 0; i < lst.size(); i++) {
				lst.get(i).setRowNum(i + 1);
			}
 
			int iCount=iAttTbService.GetAttTbListCountByWhere(where);
			layTable.setCode(0);
			layTable.setCount(iCount);
			layTable.setData(lst);
			
		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
	
	
}
